import { _decorator, Component, Node } from "cc";
import GameConst from "../../../core/common/GameConst";
import BasePanel from "../../../core/component/BasePanel";
import Data from "../../../core/manager/Data";
import WXTTUtils from "../../../core/manager/WXTTUtils";
import AudioMgr from "../../../core/utils/AudioMgr";
import Utils from "../../../core/utils/Utils";
const { ccclass, property } = _decorator;

@ccclass("BuyLifeLayer")
export class BuyLifeLayer extends BasePanel {
  update() {
    this.SetText("lbl_life", Data.user.life + "");
    if (Data.user.life >= GameConst.MAX_LIFE) {
      this.SetText("lbl_life_time", "已满");
    } else {
      if (Data.user.life_cost_time == 0) {
        Data.user.life_cost_time = Date.now();
      }

      let nLeft = (Date.now() - Data.user.life_cost_time) / 1000;
      let addLife = Math.floor(nLeft / 60 / GameConst.MAX_LIFE_RECOVERY_TIME);
      if (addLife > 0) Data.user.life += addLife;
      nLeft -= addLife * GameConst.MAX_LIFE_RECOVERY_TIME * 60;
      this.SetText(
        "lbl_life_time",
        Utils.getTimeStrByS(GameConst.MAX_LIFE_RECOVERY_TIME * 60 - nLeft)
      );
    }
  }

  star() {
    // WXTTUtils.showInterstitialAd();
  }

  onBtnClicked(event: any, customEventData: any) {
    AudioMgr.Instance().playSFX("btnclick");
    var btnName = event.target.name;
    switch (btnName) {
      case "btn_close":
        this.close();
        break;
      case "btn_ad":
        Data.user.life += 1;
        Data.save();
        this.close();
        break;
    }
  }
}
